<?php

namespace app\common\repository\article;

use app\common\model\article\Article as ArticleModel;
use think\Db;

class R
{
    public function __construct(
        ArticleModel $articleModel
    ) {
        $this->articleModel = $articleModel;
    }

    public function getArticle($cond)
    {
        return ArticleModel::where($cond)
            ->with(['tag', 'type', 'know', 'source', 'goods', 'section' => ['book']])
            ->find();
    }

    public function getArticles($cond)
    {
        return ArticleModel::where($cond)
            ->select();
    }

    public function getUserHotArticles($userId)
    {
        return ArticleModel::where(['user_id' => $userId])
            ->with(['section' => ['book']])
            ->order('view_count', 'desc')
            ->limit(5)
            ->select();
    }

    public function getMostViewArticles()
    {
        return ArticleModel::order('view_count', 'desc')
            ->with(['section' => ['book']])
            ->limit(5)
            ->select();
    }

    public function getNewestArticles()
    {
        return ArticleModel::order('create_time', 'desc')
            ->limit(5)
            ->select();
    }

    public function getArticlesWithPage($cond, $order, $page)
    {
        $count = count(ArticleModel::where($cond)
            ->select());
        $list = ArticleModel::where($cond)
            ->with(['user' => ['profile'], 'section' => ['book']])
            ->order($order)
            ->limit($page['size'])
            ->page($page['index'])
            ->select();
        return ['count' => $count, 'list' => $list];
    }

    public function getArticlesByTypeWithPage($type, $order, $page)
    {
        $cond = [];
        if ($type != 0) {
            $cond[] = ['id', 'IN', function ($query) use ($type) {
                $query->table('article_access_article_type')
                    ->alias('a')
                    ->join('article_type b', 'a.article_type_id = b.id', 'LEFT')
                    ->where('b.id', $type)
                    ->field('a.article_id as id');
            }];
        }
        $all = ArticleModel::where($cond)
            ->with(['user' => ['profile'], 'section' => ['book'], 'tag'])
            ->where($cond);
        $count = $all->count();
        $list = $all->limit($page['size'])
            ->order($order)
            ->page($page['index'])
            ->select();
        return ['count' => $count, 'list' => $list];
    }

    public function getArticlesByTagIdAndKeywodWithPage($tagId = '', $keyword = '', $order, $page)
    {
        $cond = [];
        if (!empty($tagId)) {
            $cond[] = ['id', 'IN', function ($query) use ($tagId) {
                $query->table('article_access_article_tag')
                    ->alias('a')
                    ->join('article_tag b', 'a.article_tag_id = b.id', 'LEFT')
                    ->where('b.id', $tagId)
                    ->field('a.article_id as id');
            }];
        }
        if (!empty($keyword)) {
            $cond[] = ['title', 'like', '%' . $keyword . '%'];
        }
        $all = ArticleModel::where($cond)
            ->with(['user' => ['profile'], 'section' => ['book'], 'tag'])
            ->where($cond);
        $count = $all->count();
        $list = $all->limit($page['size'])
            ->order($order)
            ->page($page['index'])
            ->select();
        return ['count' => $count, 'list' => $list];
    }

    public function getArticlesAndBooksWithPage($order, $page)
    {
        $all = Db::field('a.id, a.title, 1 as type, a.praise_count, a.comment_count')
            ->table('article')
            ->alias('a')
            ->join('user_profile b', 'b.id=a.user_id', 'LEFT')
            ->field('b.nickname as user_nickname')
            ->union(function ($query) {
                $query->field('c.id, c.title, 2 as type, c.praise_count, c.comment_count')
                    ->table('book')
                    ->alias('c')
                    ->join('user_profile d', 'd.id=c.user_id', 'LEFT')
                    ->field('d.nickname as user_nickname')
                    ->where('delete_time', 'null');
            })
            ->where('delete_time', 'null');
        $count = count($all->select());
        $list = $all->limit($page['size'])
            ->order($order)
            ->page($page['index'])
            ->select();
        return ['count' => $count, 'list' => $list];
    }
}
